{extend name="public:base"}
{block name='css'}
<link href="__PUBLIC__/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="__PUBLIC__/css/style.min862f.css" rel="stylesheet">
{/block}
{block name="js"}
<script src="__PUBLIC__/js/plugins/peity/jquery.peity.min.js"></script>
<script src="__PUBLIC__/js/content.min.js?v=1.0.0"></script>
<script>
    function execute(self) {
        var t = layer.load(1);
        $.get($(self).attr('href'), '', function(data){
            layer.close(t);
            layer.msg(data.info);
        },'json');
    }

    $(function(){
        //全选的实现
        $(".check-all").click(function(){
            $(".ids").prop("checked", this.checked);
        });
        $(".ids").click(function(){
            var option = $(".ids");
            option.each(function(i){
                if(!this.checked){
                    $(".check-all").prop("checked", false);
                    return false;
                }else{
                    $(".check-all").prop("checked", true);
                }
            });
        });

    });
</script>
<script type="text/javascript">

    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
        $optimize = $("#optimize"), $repair = $("#repair");



        $optimize.add($repair).click(function(){
            var that = $export.parent().children();
            that.addClass("disabled");
            $.post(this.href, $form.serialize(), function(data){
                layer.msg(data.info);
                setTimeout(function(){
//                    $('#top-alert').find('button').click();
                    that.removeClass('disabled').prop('disabled',false);
                },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            var that = $export.parent().children();
            that.addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status == 'y'){
                        tables = data.data.tables;
                        $export.html(data.info + "开始备份，请不要关闭本页面！");
                        backup(data.data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        layer.msg(data.info);
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
                            that.removeClass('disabled').prop('disabled',false);
                        },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            var that = $export.parent().children();
            that.addClass("disabled");
            $.get($form.attr("action"), tab, function(data){
                if(data.status == 'y'){
                    showmsg(tab.id, data.info);

                    if(!data.data.tab){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    layer.msg(data.info);
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
                        that.removeClass('disabled').prop('disabled',false);
                    },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>
{/block}
{block name='body'}
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">

        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>添加备份</h5>
                        <a href="javascript:history.go(-1);" class="btn btn-sm btn-primary pull-right m-t-n-xs">返回</a>
                    </div>
                    <div class="ibox-content">
						<div class="row">
                            <div class="col-sm-4 m-b-xs">
								<button type="button" id="export" class="btn btn-primary btn-sm" autocomplete="off">立即备份</button>
								<a id="optimize" class="btn btn-primary btn-sm" href="{:url('Database/optimize')}">优化表</a>
        						<a id="repair" class="btn btn-primary btn-sm" href="{:url('Database/repair')}">修复表</a>
                            </div>
                        </div>
						<form name="form" action="{:url('export')}" id="export-form" method="post" >
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th width="48"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                                    <th>表名</th>
                                    <th>数据量</th>
                                    <th>数据大小</th>
                                    <th>创建时间</th>
                                    <th>备份状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
								{foreach name="list" item="li"}
                                    <tr>
                                        <td class="num">
                                            <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$li.name}">
                                        </td>
                                        <td>{$li.name}</td>
                                        <td>{$li.rows}</td>
                                        <td>{$li.data_length|format_bytes=###}</td>
                                        <td>{$li.create_time}</td>
                                        <td class="info">未备份</td>
                                        <td class="action">
                                            <a class="ajax-get no-refresh btn btn-primary btn-xs" href="{:url('Database/optimize?tables='.$li['name'])}" onclick="execute(this);return false;">优化表</a>&nbsp;
                                            <a class="ajax-get no-refresh btn btn-primary btn-xs" href="{:url('Database/repair?tables='.$li['name'])}" onclick="execute(this);return false;">修复表</a>
                                        </td>
                                    </tr>
								{/foreach}
                            </tbody>
                        </table>
						</form>
                    </div>
                </div>
            </div>
        </div>
        
    </div>

</body>
{/block}